#ifndef _TEXTUREHANDLER_H
#define _TEXTUREHANDLER_H

#include "OpenGLRenderer.h"

//Standard library headers
#include <vector>
#include <iostream>

class CTextureHandler
{
	
private:
	
	struct Texture {
		//Constructor
		Texture(char* Filename, GLuint TexIndex) : filename(Filename), texindex(TexIndex)		{}
		
		//Variables
		char* filename;
		GLuint texindex;
	};
	
public:
	
	CTextureHandler()		{}
	~CTextureHandler()		{ killAllTextures(); }
	
	//Singleton instance function
	/*static ODN_TextureHandler* Instance()
	{
		static ODN_TextureHandler t;
		return &t;
	}*/
	
	//TODO: Constructor and destructor 
	
	//Main texture function. Will return texture index if already in use or load it from file if it's not 
	GLuint getTexture(char* Filename)
	{
		
		//Loop through textures
		for (int i=0;i<textures.size();i++) {
			
			//Check for texture with this filename 
			if (textures[i].filename == Filename) {
				
				//Return this texture as it is the same as the one we want 
				return textures[i].texindex;
				
			}
			
		}
		
		//Otherwise create a new texture
		return createTexture(Filename); 
		
	}
	
	//Delete all of the textures 
	void killAllTextures()
	{
		for (int i=0;i<textures.size();i++) {
			glDeleteTextures(1, &textures[i].texindex);
		}
		textures.clear(); 
	}
	
private:
	
	std::vector<Texture> textures;
	
	//Create and load a texture from a file 
	GLuint createTexture(char* Filename);
	
	//Image loading functions 
	GLuint loadTGATexture(char* Filename);
	int CopyByteArray(unsigned char* &DestArray, unsigned char* SourceArray, unsigned int DestStart, unsigned int SourceStart, unsigned int Length);
	unsigned short GetNextPowOf2(unsigned short Number);
	
};

//Return the singleton instance of the texture handler
//ODN_TextureHandler* getTextureHandler();

#endif //_TEXTUREHANDLER_H 
